package com.example.power_test;

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;

public class CPU_freq {

	private static String cat_PATH = "cat /sys/devices/system/cpu/cpu0/cpufreq/";
	private static String PATH = "/sys/devices/system/cpu/cpu0/cpufreq/";

	public static String getavailable_freq() {
		String ava_freq = "";
		try {
			Process p = Runtime.getRuntime().exec("su");
			PrintWriter dos = new PrintWriter(p.getOutputStream());
			BufferedReader br = new BufferedReader(new InputStreamReader(
					p.getInputStream()));
			dos.println(cat_PATH + "scaling_available_frequencies");
			dos.flush();
			dos.println("exit");
			dos.flush();
			p.waitFor();
			ava_freq += br.readLine();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return ava_freq;
	}

	public static String getcur_gover() {
		String cur_gover = "";
		try {
			Process p = Runtime.getRuntime().exec("su");
			PrintWriter dos = new PrintWriter(p.getOutputStream());
			BufferedReader br = new BufferedReader(new InputStreamReader(
					p.getInputStream()));
			dos.println(cat_PATH + "scaling_governor");
			dos.flush();
			dos.println("exit");
			dos.flush();
			p.waitFor();
			cur_gover += br.readLine();
			br.close();
			dos.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return cur_gover;
	}

	public static void setcur_gover(String governor) {
		try {
			Process p = Runtime.getRuntime().exec("su");
			PrintWriter dos = new PrintWriter(p.getOutputStream());
			BufferedReader br = new BufferedReader(new InputStreamReader(
					p.getInputStream()));
			dos.println("echo " + governor + ">" + PATH + "scaling_governor");
			dos.flush();
			dos.println("exit");
			dos.flush();
			p.waitFor();
			br.close();
			dos.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public static String getcur_freq() {
		String cur_freq = "";
		try {
			Process p = Runtime.getRuntime().exec("su");
			PrintWriter dos = new PrintWriter(p.getOutputStream());
			BufferedReader br = new BufferedReader(new InputStreamReader(
					p.getInputStream()));
			dos.println(cat_PATH + "cpuinfo_cur_freq");
			dos.flush();
			dos.println("exit");
			dos.flush();
			p.waitFor();
			cur_freq += br.readLine();
			dos.close();
			br.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return cur_freq;
	}

	public static void setcur_freq(String frequence) {
		Process p;
		try {
			p = Runtime.getRuntime().exec("su");
			PrintWriter dos = new PrintWriter(p.getOutputStream());
			BufferedReader br = new BufferedReader(new InputStreamReader(
					p.getInputStream()));
			dos.println("echo " + frequence + ">" + PATH + "scaling_setspeed");
			dos.flush();
			dos.println("exit");
			dos.flush();
			p.waitFor();
			dos.close();
			br.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public static long getCpuusage() {
		String[] cpuInfos1 = null;
		String[] cpuInfos2 = null;
		String load1;
		String load2;

		try {
			BufferedReader reader1 = new BufferedReader(new InputStreamReader(
					new FileInputStream("/proc/stat")), 1000);
			BufferedReader reader2 = new BufferedReader(new InputStreamReader(
					new FileInputStream("/proc/stat")), 1000);
			load1 = reader1.readLine();
			try {
				Thread.sleep(1000);
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			load2 = reader2.readLine();
			reader1.close();
			reader2.close();
			cpuInfos1 = load1.split(" ");
			cpuInfos2 = load2.split(" ");
		} catch (IOException ex) {
			// Log.e(TAG, "IOException" + ex.toString());
			return 0;
		}
		long totalCpu1 = 0;
		long totalCpu2 = 0;
		long idle1 = 0;
		long idle2 = 0;
		try {
			totalCpu1 = Long.parseLong(cpuInfos1[2])
					+ Long.parseLong(cpuInfos1[3])
					+ Long.parseLong(cpuInfos1[4])
					+ Long.parseLong(cpuInfos1[6])
					+ Long.parseLong(cpuInfos1[5])
					+ Long.parseLong(cpuInfos1[7])
					+ Long.parseLong(cpuInfos1[8]);
			idle1 = Long.parseLong(cpuInfos1[5]);
			totalCpu2 = Long.parseLong(cpuInfos2[2])
					+ Long.parseLong(cpuInfos2[3])
					+ Long.parseLong(cpuInfos2[4])
					+ Long.parseLong(cpuInfos2[6])
					+ Long.parseLong(cpuInfos2[5])
					+ Long.parseLong(cpuInfos2[7])
					+ Long.parseLong(cpuInfos2[8]);
			idle2 = Long.parseLong(cpuInfos2[5]);
		} catch (ArrayIndexOutOfBoundsException e) {
			// Log.i(TAG, "ArrayIndexOutOfBoundsException" + e.toString());
			return 0;
		}
		// System.out.println("load1"+load1);
		// System.out.println("load2"+load2);
		return 100 * (totalCpu2 - totalCpu1 - idle2 + idle1)
				/ (totalCpu2 - totalCpu1);
	}

	public static Mymsg getuse() {
		Mymsg mymsg = new Mymsg();
		String[] cpuInfos = null;
		String load;

		try {
			BufferedReader reader = new BufferedReader(new InputStreamReader(
					new FileInputStream("/proc/stat")), 1000);
			load = reader.readLine();
			reader.close();
			cpuInfos = load.split(" ");
			mymsg.totalcpu = Long.parseLong(cpuInfos[2])
					+ Long.parseLong(cpuInfos[3]) + Long.parseLong(cpuInfos[4])
					+ Long.parseLong(cpuInfos[6]) + Long.parseLong(cpuInfos[5])
					+ Long.parseLong(cpuInfos[7]) + Long.parseLong(cpuInfos[8]);
			mymsg.idle = Long.parseLong(cpuInfos[5]);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return mymsg;
	}

}
